Path: blob/master/drivers/media/video/gspca/pac7302.c
17633 views
/*1* Pixart PAC7302 library2* Copyright (C) 2005 Thomas Kaiser [email protected]3*4* V4L2 by Jean-Francois Moine <http://moinejf.free.fr>5*6* Separated from Pixart PAC7311 library by Márton Németh7* Camera button input handling by Márton Németh <[email protected]>8* Copyright (C) 2009-2010 Márton Németh <[email protected]>9*10* This program is free software; you can redistribute it and/or modify11* it under the terms of the GNU General Public License as published by12* the Free Software Foundation; either version 2 of the License, or13* any later version.14*15* This program is distributed in the hope that it will be useful,16* but WITHOUT ANY WARRANTY; without even the implied warranty of17* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the18* GNU General Public License for more details.19*20* You should have received a copy of the GNU General Public License21* along with this program; if not, write to the Free Software22* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA23*/2425/* Some documentation about various registers as determined by trial and error.2627Register page 1:2829Address Description300x78 Global control, bit 6 controls the LED (inverted)3132Register page 3:3334Address Description350x02 Clock divider 3-63, fps = 90 / val. Must be a multiple of 3 on36the 7302, so one of 3, 6, 9, ..., except when between 6 and 12?370x03 Variable framerate ctrl reg2==3: 0 -> ~30 fps, 255 -> ~22fps380x04 Another var framerate ctrl reg2==3, reg3==0: 0 -> ~30 fps,3963 -> ~27 fps, the 2 msb's must always be 1 !!400x05 Another var framerate ctrl reg2==3, reg3==0, reg4==0xc0:411 -> ~30 fps, 2 -> ~20 fps420x0e Exposure bits 0-7, 0-448, 0 = use full frame time430x0f Exposure bit 8, 0-448, 448 = no exposure at all440x10 Master gain 0-31450x21 Bitfield: 0-1 unused, 2-3 vflip/hflip, 4-5 unknown, 6-7 unused4647The registers are accessed in the following functions:4849Page | Register | Function50-----+------------+---------------------------------------------------510 | 0x0f..0x20 | setcolors()520 | 0xa2..0xab | setbrightcont()530 | 0xc5 | setredbalance()540 | 0xc6 | setwhitebalance()550 | 0xc7 | setbluebalance()560 | 0xdc | setbrightcont(), setcolors()573 | 0x02 | setexposure()583 | 0x10 | setgain()593 | 0x11 | setcolors(), setgain(), setexposure(), sethvflip()603 | 0x21 | sethvflip()61*/6263#define MODULE_NAME "pac7302"6465#include <linux/input.h>66#include <media/v4l2-chip-ident.h>67#include "gspca.h"6869MODULE_AUTHOR("Thomas Kaiser [email protected]");70MODULE_DESCRIPTION("Pixart PAC7302");71MODULE_LICENSE("GPL");7273/* specific webcam descriptor for pac7302 */74struct sd {75struct gspca_dev gspca_dev; /* !! must be the first item */7677unsigned char brightness;78unsigned char contrast;79unsigned char colors;80unsigned char white_balance;81unsigned char red_balance;82unsigned char blue_balance;83unsigned char gain;84unsigned char autogain;85unsigned short exposure;86__u8 hflip;87__u8 vflip;88u8 flags;89#define FL_HFLIP 0x01 /* mirrored by default */90#define FL_VFLIP 0x02 /* vertical flipped by default */9192u8 sof_read;93u8 autogain_ignore_frames;9495atomic_t avg_lum;96};9798/* V4L2 controls supported by the driver */99static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);100static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);101static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val);102static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val);103static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val);104static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val);105static int sd_setwhitebalance(struct gspca_dev *gspca_dev, __s32 val);106static int sd_getwhitebalance(struct gspca_dev *gspca_dev, __s32 *val);107static int sd_setredbalance(struct gspca_dev *gspca_dev, __s32 val);108static int sd_getredbalance(struct gspca_dev *gspca_dev, __s32 *val);109static int sd_setbluebalance(struct gspca_dev *gspca_dev, __s32 val);110static int sd_getbluebalance(struct gspca_dev *gspca_dev, __s32 *val);111static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val);112static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val);113static int sd_sethflip(struct gspca_dev *gspca_dev, __s32 val);114static int sd_gethflip(struct gspca_dev *gspca_dev, __s32 *val);115static int sd_setvflip(struct gspca_dev *gspca_dev, __s32 val);116static int sd_getvflip(struct gspca_dev *gspca_dev, __s32 *val);117static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val);118static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val);119static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val);120static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val);121122static const struct ctrl sd_ctrls[] = {123{124{125.id = V4L2_CID_BRIGHTNESS,126.type = V4L2_CTRL_TYPE_INTEGER,127.name = "Brightness",128.minimum = 0,129#define BRIGHTNESS_MAX 0x20130.maximum = BRIGHTNESS_MAX,131.step = 1,132#define BRIGHTNESS_DEF 0x10133.default_value = BRIGHTNESS_DEF,134},135.set = sd_setbrightness,136.get = sd_getbrightness,137},138{139{140.id = V4L2_CID_CONTRAST,141.type = V4L2_CTRL_TYPE_INTEGER,142.name = "Contrast",143.minimum = 0,144#define CONTRAST_MAX 255145.maximum = CONTRAST_MAX,146.step = 1,147#define CONTRAST_DEF 127148.default_value = CONTRAST_DEF,149},150.set = sd_setcontrast,151.get = sd_getcontrast,152},153{154{155.id = V4L2_CID_SATURATION,156.type = V4L2_CTRL_TYPE_INTEGER,157.name = "Saturation",158.minimum = 0,159#define COLOR_MAX 255160.maximum = COLOR_MAX,161.step = 1,162#define COLOR_DEF 127163.default_value = COLOR_DEF,164},165.set = sd_setcolors,166.get = sd_getcolors,167},168{169{170.id = V4L2_CID_WHITE_BALANCE_TEMPERATURE,171.type = V4L2_CTRL_TYPE_INTEGER,172.name = "White Balance",173.minimum = 0,174.maximum = 255,175.step = 1,176#define WHITEBALANCE_DEF 4177.default_value = WHITEBALANCE_DEF,178},179.set = sd_setwhitebalance,180.get = sd_getwhitebalance,181},182{183{184.id = V4L2_CID_RED_BALANCE,185.type = V4L2_CTRL_TYPE_INTEGER,186.name = "Red",187.minimum = 0,188.maximum = 3,189.step = 1,190#define REDBALANCE_DEF 1191.default_value = REDBALANCE_DEF,192},193.set = sd_setredbalance,194.get = sd_getredbalance,195},196{197{198.id = V4L2_CID_BLUE_BALANCE,199.type = V4L2_CTRL_TYPE_INTEGER,200.name = "Blue",201.minimum = 0,202.maximum = 3,203.step = 1,204#define BLUEBALANCE_DEF 1205.default_value = BLUEBALANCE_DEF,206},207.set = sd_setbluebalance,208.get = sd_getbluebalance,209},210{211{212.id = V4L2_CID_GAIN,213.type = V4L2_CTRL_TYPE_INTEGER,214.name = "Gain",215.minimum = 0,216#define GAIN_MAX 255217.maximum = GAIN_MAX,218.step = 1,219#define GAIN_DEF 127220#define GAIN_KNEE 255 /* Gain seems to cause little noise on the pac73xx */221.default_value = GAIN_DEF,222},223.set = sd_setgain,224.get = sd_getgain,225},226{227{228.id = V4L2_CID_EXPOSURE,229.type = V4L2_CTRL_TYPE_INTEGER,230.name = "Exposure",231.minimum = 0,232.maximum = 1023,233.step = 1,234#define EXPOSURE_DEF 66 /* 33 ms / 30 fps */235#define EXPOSURE_KNEE 133 /* 66 ms / 15 fps */236.default_value = EXPOSURE_DEF,237},238.set = sd_setexposure,239.get = sd_getexposure,240},241{242{243.id = V4L2_CID_AUTOGAIN,244.type = V4L2_CTRL_TYPE_BOOLEAN,245.name = "Auto Gain",246.minimum = 0,247.maximum = 1,248.step = 1,249#define AUTOGAIN_DEF 1250.default_value = AUTOGAIN_DEF,251},252.set = sd_setautogain,253.get = sd_getautogain,254},255{256{257.id = V4L2_CID_HFLIP,258.type = V4L2_CTRL_TYPE_BOOLEAN,259.name = "Mirror",260.minimum = 0,261.maximum = 1,262.step = 1,263#define HFLIP_DEF 0264.default_value = HFLIP_DEF,265},266.set = sd_sethflip,267.get = sd_gethflip,268},269{270{271.id = V4L2_CID_VFLIP,272.type = V4L2_CTRL_TYPE_BOOLEAN,273.name = "Vflip",274.minimum = 0,275.maximum = 1,276.step = 1,277#define VFLIP_DEF 0278.default_value = VFLIP_DEF,279},280.set = sd_setvflip,281.get = sd_getvflip,282},283};284285static const struct v4l2_pix_format vga_mode[] = {286{640, 480, V4L2_PIX_FMT_PJPG, V4L2_FIELD_NONE,287.bytesperline = 640,288.sizeimage = 640 * 480 * 3 / 8 + 590,289.colorspace = V4L2_COLORSPACE_JPEG,290.priv = 0},291};292293#define LOAD_PAGE3 255294#define END_OF_SEQUENCE 0295296/* pac 7302 */297static const __u8 init_7302[] = {298/* index,value */2990xff, 0x01, /* page 1 */3000x78, 0x00, /* deactivate */3010xff, 0x01,3020x78, 0x40, /* led off */303};304static const __u8 start_7302[] = {305/* index, len, [value]* */3060xff, 1, 0x00, /* page 0 */3070x00, 12, 0x01, 0x40, 0x40, 0x40, 0x01, 0xe0, 0x02, 0x80,3080x00, 0x00, 0x00, 0x00,3090x0d, 24, 0x03, 0x01, 0x00, 0xb5, 0x07, 0xcb, 0x00, 0x00,3100x07, 0xc8, 0x00, 0xea, 0x07, 0xcf, 0x07, 0xf7,3110x07, 0x7e, 0x01, 0x0b, 0x00, 0x00, 0x00, 0x11,3120x26, 2, 0xaa, 0xaa,3130x2e, 1, 0x31,3140x38, 1, 0x01,3150x3a, 3, 0x14, 0xff, 0x5a,3160x43, 11, 0x00, 0x0a, 0x18, 0x11, 0x01, 0x2c, 0x88, 0x11,3170x00, 0x54, 0x11,3180x55, 1, 0x00,3190x62, 4, 0x10, 0x1e, 0x1e, 0x18,3200x6b, 1, 0x00,3210x6e, 3, 0x08, 0x06, 0x00,3220x72, 3, 0x00, 0xff, 0x00,3230x7d, 23, 0x01, 0x01, 0x58, 0x46, 0x50, 0x3c, 0x50, 0x3c,3240x54, 0x46, 0x54, 0x56, 0x52, 0x50, 0x52, 0x50,3250x56, 0x64, 0xa4, 0x00, 0xda, 0x00, 0x00,3260xa2, 10, 0x22, 0x2c, 0x3c, 0x54, 0x69, 0x7c, 0x9c, 0xb9,3270xd2, 0xeb,3280xaf, 1, 0x02,3290xb5, 2, 0x08, 0x08,3300xb8, 2, 0x08, 0x88,3310xc4, 4, 0xae, 0x01, 0x04, 0x01,3320xcc, 1, 0x00,3330xd1, 11, 0x01, 0x30, 0x49, 0x5e, 0x6f, 0x7f, 0x8e, 0xa9,3340xc1, 0xd7, 0xec,3350xdc, 1, 0x01,3360xff, 1, 0x01, /* page 1 */3370x12, 3, 0x02, 0x00, 0x01,3380x3e, 2, 0x00, 0x00,3390x76, 5, 0x01, 0x20, 0x40, 0x00, 0xf2,3400x7c, 1, 0x00,3410x7f, 10, 0x4b, 0x0f, 0x01, 0x2c, 0x02, 0x58, 0x03, 0x20,3420x02, 0x00,3430x96, 5, 0x01, 0x10, 0x04, 0x01, 0x04,3440xc8, 14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00,3450x07, 0x00, 0x01, 0x07, 0x04, 0x01,3460xd8, 1, 0x01,3470xdb, 2, 0x00, 0x01,3480xde, 7, 0x00, 0x01, 0x04, 0x04, 0x00, 0x00, 0x00,3490xe6, 4, 0x00, 0x00, 0x00, 0x01,3500xeb, 1, 0x00,3510xff, 1, 0x02, /* page 2 */3520x22, 1, 0x00,3530xff, 1, 0x03, /* page 3 */3540, LOAD_PAGE3, /* load the page 3 */3550x11, 1, 0x01,3560xff, 1, 0x02, /* page 2 */3570x13, 1, 0x00,3580x22, 4, 0x1f, 0xa4, 0xf0, 0x96,3590x27, 2, 0x14, 0x0c,3600x2a, 5, 0xc8, 0x00, 0x18, 0x12, 0x22,3610x64, 8, 0x00, 0x00, 0xf0, 0x01, 0x14, 0x44, 0x44, 0x44,3620x6e, 1, 0x08,3630xff, 1, 0x01, /* page 1 */3640x78, 1, 0x00,3650, END_OF_SEQUENCE /* end of sequence */366};367368#define SKIP 0xaa369/* page 3 - the value SKIP says skip the index - see reg_w_page() */370static const __u8 page3_7302[] = {3710x90, 0x40, 0x03, 0x00, 0xc0, 0x01, 0x14, 0x16,3720x14, 0x12, 0x00, 0x00, 0x00, 0x02, 0x33, 0x00,3730x0f, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,3740x00, 0x00, 0x00, 0x47, 0x01, 0xb3, 0x01, 0x00,3750x00, 0x08, 0x00, 0x00, 0x0d, 0x00, 0x00, 0x21,3760x00, 0x00, 0x00, 0x54, 0xf4, 0x02, 0x52, 0x54,3770xa4, 0xb8, 0xe0, 0x2a, 0xf6, 0x00, 0x00, 0x00,3780x00, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,3790x00, 0xfc, 0x00, 0xf2, 0x1f, 0x04, 0x00, 0x00,380SKIP, 0x00, 0x00, 0xc0, 0xc0, 0x10, 0x00, 0x00,3810x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,3820x00, 0x40, 0xff, 0x03, 0x19, 0x00, 0x00, 0x00,3830x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,3840x00, 0x00, 0x00, 0x00, 0x00, 0xc8, 0xc8, 0xc8,3850xc8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50,3860x08, 0x10, 0x24, 0x40, 0x00, 0x00, 0x00, 0x00,3870x01, 0x00, 0x02, 0x47, 0x00, 0x00, 0x00, 0x00,3880x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,3890x00, 0x02, 0xfa, 0x00, 0x64, 0x5a, 0x28, 0x00,3900x00391};392393static void reg_w_buf(struct gspca_dev *gspca_dev,394__u8 index,395const u8 *buffer, int len)396{397int ret;398399if (gspca_dev->usb_err < 0)400return;401memcpy(gspca_dev->usb_buf, buffer, len);402ret = usb_control_msg(gspca_dev->dev,403usb_sndctrlpipe(gspca_dev->dev, 0),4040, /* request */405USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,4060, /* value */407index, gspca_dev->usb_buf, len,408500);409if (ret < 0) {410err("reg_w_buf failed index 0x%02x, error %d",411index, ret);412gspca_dev->usb_err = ret;413}414}415416417static void reg_w(struct gspca_dev *gspca_dev,418__u8 index,419__u8 value)420{421int ret;422423if (gspca_dev->usb_err < 0)424return;425gspca_dev->usb_buf[0] = value;426ret = usb_control_msg(gspca_dev->dev,427usb_sndctrlpipe(gspca_dev->dev, 0),4280, /* request */429USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,4300, index, gspca_dev->usb_buf, 1,431500);432if (ret < 0) {433err("reg_w() failed index 0x%02x, value 0x%02x, error %d",434index, value, ret);435gspca_dev->usb_err = ret;436}437}438439static void reg_w_seq(struct gspca_dev *gspca_dev,440const __u8 *seq, int len)441{442while (--len >= 0) {443reg_w(gspca_dev, seq[0], seq[1]);444seq += 2;445}446}447448/* load the beginning of a page */449static void reg_w_page(struct gspca_dev *gspca_dev,450const __u8 *page, int len)451{452int index;453int ret = 0;454455if (gspca_dev->usb_err < 0)456return;457for (index = 0; index < len; index++) {458if (page[index] == SKIP) /* skip this index */459continue;460gspca_dev->usb_buf[0] = page[index];461ret = usb_control_msg(gspca_dev->dev,462usb_sndctrlpipe(gspca_dev->dev, 0),4630, /* request */464USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,4650, index, gspca_dev->usb_buf, 1,466500);467if (ret < 0) {468err("reg_w_page() failed index 0x%02x, "469"value 0x%02x, error %d",470index, page[index], ret);471gspca_dev->usb_err = ret;472break;473}474}475}476477/* output a variable sequence */478static void reg_w_var(struct gspca_dev *gspca_dev,479const __u8 *seq,480const __u8 *page3, unsigned int page3_len)481{482int index, len;483484for (;;) {485index = *seq++;486len = *seq++;487switch (len) {488case END_OF_SEQUENCE:489return;490case LOAD_PAGE3:491reg_w_page(gspca_dev, page3, page3_len);492break;493default:494if (len > USB_BUF_SZ) {495PDEBUG(D_ERR|D_STREAM,496"Incorrect variable sequence");497return;498}499while (len > 0) {500if (len < 8) {501reg_w_buf(gspca_dev,502index, seq, len);503seq += len;504break;505}506reg_w_buf(gspca_dev, index, seq, 8);507seq += 8;508index += 8;509len -= 8;510}511}512}513/* not reached */514}515516/* this function is called at probe time for pac7302 */517static int sd_config(struct gspca_dev *gspca_dev,518const struct usb_device_id *id)519{520struct sd *sd = (struct sd *) gspca_dev;521struct cam *cam;522523cam = &gspca_dev->cam;524525PDEBUG(D_CONF, "Find Sensor PAC7302");526cam->cam_mode = vga_mode; /* only 640x480 */527cam->nmodes = ARRAY_SIZE(vga_mode);528529sd->brightness = BRIGHTNESS_DEF;530sd->contrast = CONTRAST_DEF;531sd->colors = COLOR_DEF;532sd->white_balance = WHITEBALANCE_DEF;533sd->red_balance = REDBALANCE_DEF;534sd->blue_balance = BLUEBALANCE_DEF;535sd->gain = GAIN_DEF;536sd->exposure = EXPOSURE_DEF;537sd->autogain = AUTOGAIN_DEF;538sd->hflip = HFLIP_DEF;539sd->vflip = VFLIP_DEF;540sd->flags = id->driver_info;541return 0;542}543544/* This function is used by pac7302 only */545static void setbrightcont(struct gspca_dev *gspca_dev)546{547struct sd *sd = (struct sd *) gspca_dev;548int i, v;549static const __u8 max[10] =550{0x29, 0x33, 0x42, 0x5a, 0x6e, 0x80, 0x9f, 0xbb,5510xd4, 0xec};552static const __u8 delta[10] =553{0x35, 0x33, 0x33, 0x2f, 0x2a, 0x25, 0x1e, 0x17,5540x11, 0x0b};555556reg_w(gspca_dev, 0xff, 0x00); /* page 0 */557for (i = 0; i < 10; i++) {558v = max[i];559v += (sd->brightness - BRIGHTNESS_MAX)560* 150 / BRIGHTNESS_MAX; /* 200 ? */561v -= delta[i] * sd->contrast / CONTRAST_MAX;562if (v < 0)563v = 0;564else if (v > 0xff)565v = 0xff;566reg_w(gspca_dev, 0xa2 + i, v);567}568reg_w(gspca_dev, 0xdc, 0x01);569}570571/* This function is used by pac7302 only */572static void setcolors(struct gspca_dev *gspca_dev)573{574struct sd *sd = (struct sd *) gspca_dev;575int i, v;576static const int a[9] =577{217, -212, 0, -101, 170, -67, -38, -315, 355};578static const int b[9] =579{19, 106, 0, 19, 106, 1, 19, 106, 1};580581reg_w(gspca_dev, 0xff, 0x03); /* page 3 */582reg_w(gspca_dev, 0x11, 0x01);583reg_w(gspca_dev, 0xff, 0x00); /* page 0 */584for (i = 0; i < 9; i++) {585v = a[i] * sd->colors / COLOR_MAX + b[i];586reg_w(gspca_dev, 0x0f + 2 * i, (v >> 8) & 0x07);587reg_w(gspca_dev, 0x0f + 2 * i + 1, v);588}589reg_w(gspca_dev, 0xdc, 0x01);590PDEBUG(D_CONF|D_STREAM, "color: %i", sd->colors);591}592593static void setwhitebalance(struct gspca_dev *gspca_dev)594{595struct sd *sd = (struct sd *) gspca_dev;596597reg_w(gspca_dev, 0xff, 0x00); /* page 0 */598reg_w(gspca_dev, 0xc6, sd->white_balance);599600reg_w(gspca_dev, 0xdc, 0x01);601PDEBUG(D_CONF|D_STREAM, "white_balance: %i", sd->white_balance);602}603604static void setredbalance(struct gspca_dev *gspca_dev)605{606struct sd *sd = (struct sd *) gspca_dev;607608reg_w(gspca_dev, 0xff, 0x00); /* page 0 */609reg_w(gspca_dev, 0xc5, sd->red_balance);610611reg_w(gspca_dev, 0xdc, 0x01);612PDEBUG(D_CONF|D_STREAM, "red_balance: %i", sd->red_balance);613}614615static void setbluebalance(struct gspca_dev *gspca_dev)616{617struct sd *sd = (struct sd *) gspca_dev;618619reg_w(gspca_dev, 0xff, 0x00); /* page 0 */620reg_w(gspca_dev, 0xc7, sd->blue_balance);621622reg_w(gspca_dev, 0xdc, 0x01);623PDEBUG(D_CONF|D_STREAM, "blue_balance: %i", sd->blue_balance);624}625626static void setgain(struct gspca_dev *gspca_dev)627{628struct sd *sd = (struct sd *) gspca_dev;629630reg_w(gspca_dev, 0xff, 0x03); /* page 3 */631reg_w(gspca_dev, 0x10, sd->gain >> 3);632633/* load registers to sensor (Bit 0, auto clear) */634reg_w(gspca_dev, 0x11, 0x01);635}636637static void setexposure(struct gspca_dev *gspca_dev)638{639struct sd *sd = (struct sd *) gspca_dev;640__u8 clockdiv;641__u16 exposure;642643/* register 2 of frame 3 contains the clock divider configuring the644no fps according to the formula: 90 / reg. sd->exposure is the645desired exposure time in 0.5 ms. */646clockdiv = (90 * sd->exposure + 1999) / 2000;647648/* Note clockdiv = 3 also works, but when running at 30 fps, depending649on the scene being recorded, the camera switches to another650quantization table for certain JPEG blocks, and we don't know how651to decompress these blocks. So we cap the framerate at 15 fps */652if (clockdiv < 6)653clockdiv = 6;654else if (clockdiv > 63)655clockdiv = 63;656657/* reg2 MUST be a multiple of 3, except when between 6 and 12?658Always round up, otherwise we cannot get the desired frametime659using the partial frame time exposure control */660if (clockdiv < 6 || clockdiv > 12)661clockdiv = ((clockdiv + 2) / 3) * 3;662663/* frame exposure time in ms = 1000 * clockdiv / 90 ->664exposure = (sd->exposure / 2) * 448 / (1000 * clockdiv / 90) */665exposure = (sd->exposure * 45 * 448) / (1000 * clockdiv);666/* 0 = use full frametime, 448 = no exposure, reverse it */667exposure = 448 - exposure;668669reg_w(gspca_dev, 0xff, 0x03); /* page 3 */670reg_w(gspca_dev, 0x02, clockdiv);671reg_w(gspca_dev, 0x0e, exposure & 0xff);672reg_w(gspca_dev, 0x0f, exposure >> 8);673674/* load registers to sensor (Bit 0, auto clear) */675reg_w(gspca_dev, 0x11, 0x01);676}677678static void sethvflip(struct gspca_dev *gspca_dev)679{680struct sd *sd = (struct sd *) gspca_dev;681u8 data, hflip, vflip;682683hflip = sd->hflip;684if (sd->flags & FL_HFLIP)685hflip = !hflip;686vflip = sd->vflip;687if (sd->flags & FL_VFLIP)688vflip = !vflip;689690reg_w(gspca_dev, 0xff, 0x03); /* page 3 */691data = (hflip ? 0x08 : 0x00) | (vflip ? 0x04 : 0x00);692reg_w(gspca_dev, 0x21, data);693694/* load registers to sensor (Bit 0, auto clear) */695reg_w(gspca_dev, 0x11, 0x01);696}697698/* this function is called at probe and resume time for pac7302 */699static int sd_init(struct gspca_dev *gspca_dev)700{701reg_w_seq(gspca_dev, init_7302, sizeof(init_7302)/2);702return gspca_dev->usb_err;703}704705static int sd_start(struct gspca_dev *gspca_dev)706{707struct sd *sd = (struct sd *) gspca_dev;708709sd->sof_read = 0;710711reg_w_var(gspca_dev, start_7302,712page3_7302, sizeof(page3_7302));713setbrightcont(gspca_dev);714setcolors(gspca_dev);715setwhitebalance(gspca_dev);716setredbalance(gspca_dev);717setbluebalance(gspca_dev);718setgain(gspca_dev);719setexposure(gspca_dev);720sethvflip(gspca_dev);721722/* only resolution 640x480 is supported for pac7302 */723724sd->sof_read = 0;725sd->autogain_ignore_frames = 0;726atomic_set(&sd->avg_lum, -1);727728/* start stream */729reg_w(gspca_dev, 0xff, 0x01);730reg_w(gspca_dev, 0x78, 0x01);731732return gspca_dev->usb_err;733}734735static void sd_stopN(struct gspca_dev *gspca_dev)736{737738/* stop stream */739reg_w(gspca_dev, 0xff, 0x01);740reg_w(gspca_dev, 0x78, 0x00);741}742743/* called on streamoff with alt 0 and on disconnect for pac7302 */744static void sd_stop0(struct gspca_dev *gspca_dev)745{746if (!gspca_dev->present)747return;748reg_w(gspca_dev, 0xff, 0x01);749reg_w(gspca_dev, 0x78, 0x40);750}751752/* Include pac common sof detection functions */753#include "pac_common.h"754755static void do_autogain(struct gspca_dev *gspca_dev)756{757struct sd *sd = (struct sd *) gspca_dev;758int avg_lum = atomic_read(&sd->avg_lum);759int desired_lum;760const int deadzone = 30;761762if (avg_lum == -1)763return;764765desired_lum = 270 + sd->brightness;766767if (sd->autogain_ignore_frames > 0)768sd->autogain_ignore_frames--;769else if (gspca_auto_gain_n_exposure(gspca_dev, avg_lum, desired_lum,770deadzone, GAIN_KNEE, EXPOSURE_KNEE))771sd->autogain_ignore_frames = PAC_AUTOGAIN_IGNORE_FRAMES;772}773774/* JPEG header, part 1 */775static const unsigned char pac_jpeg_header1[] = {7760xff, 0xd8, /* SOI: Start of Image */7777780xff, 0xc0, /* SOF0: Start of Frame (Baseline DCT) */7790x00, 0x11, /* length = 17 bytes (including this length field) */7800x08 /* Precision: 8 */781/* 2 bytes is placed here: number of image lines */782/* 2 bytes is placed here: samples per line */783};784785/* JPEG header, continued */786static const unsigned char pac_jpeg_header2[] = {7870x03, /* Number of image components: 3 */7880x01, 0x21, 0x00, /* ID=1, Subsampling 1x1, Quantization table: 0 */7890x02, 0x11, 0x01, /* ID=2, Subsampling 2x1, Quantization table: 1 */7900x03, 0x11, 0x01, /* ID=3, Subsampling 2x1, Quantization table: 1 */7917920xff, 0xda, /* SOS: Start Of Scan */7930x00, 0x0c, /* length = 12 bytes (including this length field) */7940x03, /* number of components: 3 */7950x01, 0x00, /* selector 1, table 0x00 */7960x02, 0x11, /* selector 2, table 0x11 */7970x03, 0x11, /* selector 3, table 0x11 */7980x00, 0x3f, /* Spectral selection: 0 .. 63 */7990x00 /* Successive approximation: 0 */800};801802static void pac_start_frame(struct gspca_dev *gspca_dev,803__u16 lines, __u16 samples_per_line)804{805unsigned char tmpbuf[4];806807gspca_frame_add(gspca_dev, FIRST_PACKET,808pac_jpeg_header1, sizeof(pac_jpeg_header1));809810tmpbuf[0] = lines >> 8;811tmpbuf[1] = lines & 0xff;812tmpbuf[2] = samples_per_line >> 8;813tmpbuf[3] = samples_per_line & 0xff;814815gspca_frame_add(gspca_dev, INTER_PACKET,816tmpbuf, sizeof(tmpbuf));817gspca_frame_add(gspca_dev, INTER_PACKET,818pac_jpeg_header2, sizeof(pac_jpeg_header2));819}820821/* this function is run at interrupt level */822static void sd_pkt_scan(struct gspca_dev *gspca_dev,823u8 *data, /* isoc packet */824int len) /* iso packet length */825{826struct sd *sd = (struct sd *) gspca_dev;827u8 *image;828unsigned char *sof;829830sof = pac_find_sof(&sd->sof_read, data, len);831if (sof) {832int n, lum_offset, footer_length;833834/* 6 bytes after the FF D9 EOF marker a number of lumination835bytes are send corresponding to different parts of the836image, the 14th and 15th byte after the EOF seem to837correspond to the center of the image */838lum_offset = 61 + sizeof pac_sof_marker;839footer_length = 74;840841/* Finish decoding current frame */842n = (sof - data) - (footer_length + sizeof pac_sof_marker);843if (n < 0) {844gspca_dev->image_len += n;845n = 0;846} else {847gspca_frame_add(gspca_dev, INTER_PACKET, data, n);848}849850image = gspca_dev->image;851if (image != NULL852&& image[gspca_dev->image_len - 2] == 0xff853&& image[gspca_dev->image_len - 1] == 0xd9)854gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);855856n = sof - data;857len -= n;858data = sof;859860/* Get average lumination */861if (gspca_dev->last_packet_type == LAST_PACKET &&862n >= lum_offset)863atomic_set(&sd->avg_lum, data[-lum_offset] +864data[-lum_offset + 1]);865else866atomic_set(&sd->avg_lum, -1);867868/* Start the new frame with the jpeg header */869/* The PAC7302 has the image rotated 90 degrees */870pac_start_frame(gspca_dev,871gspca_dev->width, gspca_dev->height);872}873gspca_frame_add(gspca_dev, INTER_PACKET, data, len);874}875876static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)877{878struct sd *sd = (struct sd *) gspca_dev;879880sd->brightness = val;881if (gspca_dev->streaming)882setbrightcont(gspca_dev);883return gspca_dev->usb_err;884}885886static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)887{888struct sd *sd = (struct sd *) gspca_dev;889890*val = sd->brightness;891return 0;892}893894static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)895{896struct sd *sd = (struct sd *) gspca_dev;897898sd->contrast = val;899if (gspca_dev->streaming)900setbrightcont(gspca_dev);901return gspca_dev->usb_err;902}903904static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)905{906struct sd *sd = (struct sd *) gspca_dev;907908*val = sd->contrast;909return 0;910}911912static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val)913{914struct sd *sd = (struct sd *) gspca_dev;915916sd->colors = val;917if (gspca_dev->streaming)918setcolors(gspca_dev);919return gspca_dev->usb_err;920}921922static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val)923{924struct sd *sd = (struct sd *) gspca_dev;925926*val = sd->colors;927return 0;928}929930static int sd_setwhitebalance(struct gspca_dev *gspca_dev, __s32 val)931{932struct sd *sd = (struct sd *) gspca_dev;933934sd->white_balance = val;935if (gspca_dev->streaming)936setwhitebalance(gspca_dev);937return gspca_dev->usb_err;938}939940static int sd_getwhitebalance(struct gspca_dev *gspca_dev, __s32 *val)941{942struct sd *sd = (struct sd *) gspca_dev;943944*val = sd->white_balance;945return 0;946}947948static int sd_setredbalance(struct gspca_dev *gspca_dev, __s32 val)949{950struct sd *sd = (struct sd *) gspca_dev;951952sd->red_balance = val;953if (gspca_dev->streaming)954setredbalance(gspca_dev);955return gspca_dev->usb_err;956}957958static int sd_getredbalance(struct gspca_dev *gspca_dev, __s32 *val)959{960struct sd *sd = (struct sd *) gspca_dev;961962*val = sd->red_balance;963return 0;964}965966static int sd_setbluebalance(struct gspca_dev *gspca_dev, __s32 val)967{968struct sd *sd = (struct sd *) gspca_dev;969970sd->blue_balance = val;971if (gspca_dev->streaming)972setbluebalance(gspca_dev);973return gspca_dev->usb_err;974}975976static int sd_getbluebalance(struct gspca_dev *gspca_dev, __s32 *val)977{978struct sd *sd = (struct sd *) gspca_dev;979980*val = sd->blue_balance;981return 0;982}983984static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val)985{986struct sd *sd = (struct sd *) gspca_dev;987988sd->gain = val;989if (gspca_dev->streaming)990setgain(gspca_dev);991return gspca_dev->usb_err;992}993994static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val)995{996struct sd *sd = (struct sd *) gspca_dev;997998*val = sd->gain;999return 0;1000}10011002static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val)1003{1004struct sd *sd = (struct sd *) gspca_dev;10051006sd->exposure = val;1007if (gspca_dev->streaming)1008setexposure(gspca_dev);1009return gspca_dev->usb_err;1010}10111012static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val)1013{1014struct sd *sd = (struct sd *) gspca_dev;10151016*val = sd->exposure;1017return 0;1018}10191020static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val)1021{1022struct sd *sd = (struct sd *) gspca_dev;10231024sd->autogain = val;1025/* when switching to autogain set defaults to make sure1026we are on a valid point of the autogain gain /1027exposure knee graph, and give this change time to1028take effect before doing autogain. */1029if (sd->autogain) {1030sd->exposure = EXPOSURE_DEF;1031sd->gain = GAIN_DEF;1032if (gspca_dev->streaming) {1033sd->autogain_ignore_frames =1034PAC_AUTOGAIN_IGNORE_FRAMES;1035setexposure(gspca_dev);1036setgain(gspca_dev);1037}1038}10391040return gspca_dev->usb_err;1041}10421043static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val)1044{1045struct sd *sd = (struct sd *) gspca_dev;10461047*val = sd->autogain;1048return 0;1049}10501051static int sd_sethflip(struct gspca_dev *gspca_dev, __s32 val)1052{1053struct sd *sd = (struct sd *) gspca_dev;10541055sd->hflip = val;1056if (gspca_dev->streaming)1057sethvflip(gspca_dev);1058return gspca_dev->usb_err;1059}10601061static int sd_gethflip(struct gspca_dev *gspca_dev, __s32 *val)1062{1063struct sd *sd = (struct sd *) gspca_dev;10641065*val = sd->hflip;1066return 0;1067}10681069static int sd_setvflip(struct gspca_dev *gspca_dev, __s32 val)1070{1071struct sd *sd = (struct sd *) gspca_dev;10721073sd->vflip = val;1074if (gspca_dev->streaming)1075sethvflip(gspca_dev);1076return gspca_dev->usb_err;1077}10781079static int sd_getvflip(struct gspca_dev *gspca_dev, __s32 *val)1080{1081struct sd *sd = (struct sd *) gspca_dev;10821083*val = sd->vflip;1084return 0;1085}10861087#ifdef CONFIG_VIDEO_ADV_DEBUG1088static int sd_dbg_s_register(struct gspca_dev *gspca_dev,1089struct v4l2_dbg_register *reg)1090{1091__u8 index;1092__u8 value;10931094/* reg->reg: bit0..15: reserved for register index (wIndex is 16bit1095long on the USB bus)1096*/1097if (reg->match.type == V4L2_CHIP_MATCH_HOST &&1098reg->match.addr == 0 &&1099(reg->reg < 0x000000ff) &&1100(reg->val <= 0x000000ff)1101) {1102/* Currently writing to page 0 is only supported. */1103/* reg_w() only supports 8bit index */1104index = reg->reg & 0x000000ff;1105value = reg->val & 0x000000ff;11061107/* Note that there shall be no access to other page1108by any other function between the page swith and1109the actual register write */1110reg_w(gspca_dev, 0xff, 0x00); /* page 0 */1111reg_w(gspca_dev, index, value);11121113reg_w(gspca_dev, 0xdc, 0x01);1114}1115return gspca_dev->usb_err;1116}11171118static int sd_chip_ident(struct gspca_dev *gspca_dev,1119struct v4l2_dbg_chip_ident *chip)1120{1121int ret = -EINVAL;11221123if (chip->match.type == V4L2_CHIP_MATCH_HOST &&1124chip->match.addr == 0) {1125chip->revision = 0;1126chip->ident = V4L2_IDENT_UNKNOWN;1127ret = 0;1128}1129return ret;1130}1131#endif11321133#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)1134static int sd_int_pkt_scan(struct gspca_dev *gspca_dev,1135u8 *data, /* interrupt packet data */1136int len) /* interrput packet length */1137{1138int ret = -EINVAL;1139u8 data0, data1;11401141if (len == 2) {1142data0 = data[0];1143data1 = data[1];1144if ((data0 == 0x00 && data1 == 0x11) ||1145(data0 == 0x22 && data1 == 0x33) ||1146(data0 == 0x44 && data1 == 0x55) ||1147(data0 == 0x66 && data1 == 0x77) ||1148(data0 == 0x88 && data1 == 0x99) ||1149(data0 == 0xaa && data1 == 0xbb) ||1150(data0 == 0xcc && data1 == 0xdd) ||1151(data0 == 0xee && data1 == 0xff)) {1152input_report_key(gspca_dev->input_dev, KEY_CAMERA, 1);1153input_sync(gspca_dev->input_dev);1154input_report_key(gspca_dev->input_dev, KEY_CAMERA, 0);1155input_sync(gspca_dev->input_dev);1156ret = 0;1157}1158}11591160return ret;1161}1162#endif11631164/* sub-driver description for pac7302 */1165static const struct sd_desc sd_desc = {1166.name = MODULE_NAME,1167.ctrls = sd_ctrls,1168.nctrls = ARRAY_SIZE(sd_ctrls),1169.config = sd_config,1170.init = sd_init,1171.start = sd_start,1172.stopN = sd_stopN,1173.stop0 = sd_stop0,1174.pkt_scan = sd_pkt_scan,1175.dq_callback = do_autogain,1176#ifdef CONFIG_VIDEO_ADV_DEBUG1177.set_register = sd_dbg_s_register,1178.get_chip_ident = sd_chip_ident,1179#endif1180#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)1181.int_pkt_scan = sd_int_pkt_scan,1182#endif1183};11841185/* -- module initialisation -- */1186static const struct usb_device_id device_table[] = {1187{USB_DEVICE(0x06f8, 0x3009)},1188{USB_DEVICE(0x093a, 0x2620)},1189{USB_DEVICE(0x093a, 0x2621)},1190{USB_DEVICE(0x093a, 0x2622), .driver_info = FL_VFLIP},1191{USB_DEVICE(0x093a, 0x2624), .driver_info = FL_VFLIP},1192{USB_DEVICE(0x093a, 0x2625)},1193{USB_DEVICE(0x093a, 0x2626)},1194{USB_DEVICE(0x093a, 0x2628)},1195{USB_DEVICE(0x093a, 0x2629), .driver_info = FL_VFLIP},1196{USB_DEVICE(0x093a, 0x262a)},1197{USB_DEVICE(0x093a, 0x262c)},1198{}1199};1200MODULE_DEVICE_TABLE(usb, device_table);12011202/* -- device connect -- */1203static int sd_probe(struct usb_interface *intf,1204const struct usb_device_id *id)1205{1206return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),1207THIS_MODULE);1208}12091210static struct usb_driver sd_driver = {1211.name = MODULE_NAME,1212.id_table = device_table,1213.probe = sd_probe,1214.disconnect = gspca_disconnect,1215#ifdef CONFIG_PM1216.suspend = gspca_suspend,1217.resume = gspca_resume,1218#endif1219};12201221/* -- module insert / remove -- */1222static int __init sd_mod_init(void)1223{1224return usb_register(&sd_driver);1225}1226static void __exit sd_mod_exit(void)1227{1228usb_deregister(&sd_driver);1229}12301231module_init(sd_mod_init);1232module_exit(sd_mod_exit);123312341235