2019-05-19 15:51:54 +02:00
// SPDX-License-Identifier: GPL-2.0-or-later
2008-04-12 09:58:09 -03:00
/*
* Syntek DV4000 ( STK014 ) subdriver
*
2008-06-12 10:58:58 -03:00
* Copyright ( C ) 2008 Jean - Francois Moine ( http : //moinejf.free.fr)
2008-04-12 09:58:09 -03:00
*/
2011-08-21 19:56:57 -03:00
# define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
2008-04-12 09:58:09 -03:00
# define MODULE_NAME "stk014"
# include "gspca.h"
# include "jpeg.h"
MODULE_AUTHOR ( " Jean-Francois Moine <http://moinejf.free.fr> " ) ;
MODULE_DESCRIPTION ( " Syntek DV4000 (STK014) USB Camera Driver " ) ;
MODULE_LICENSE ( " GPL " ) ;
2012-06-27 16:48:33 -03:00
# define QUALITY 50
2008-04-12 09:58:09 -03:00
/* specific webcam descriptor */
struct sd {
struct gspca_dev gspca_dev ; /* !! must be the first item */
2010-06-04 05:26:42 -03:00
u8 jpeg_hdr [ JPEG_HDR_SZ ] ;
2008-04-12 09:58:09 -03:00
} ;
2008-12-29 07:49:41 -03:00
static const struct v4l2_pix_format vga_mode [ ] = {
2008-07-05 11:49:20 -03:00
{ 320 , 240 , V4L2_PIX_FMT_JPEG , V4L2_FIELD_NONE ,
. bytesperline = 320 ,
. sizeimage = 320 * 240 * 3 / 8 + 590 ,
. colorspace = V4L2_COLORSPACE_JPEG ,
. priv = 1 } ,
{ 640 , 480 , V4L2_PIX_FMT_JPEG , V4L2_FIELD_NONE ,
. bytesperline = 640 ,
. sizeimage = 640 * 480 * 3 / 8 + 590 ,
. colorspace = V4L2_COLORSPACE_JPEG ,
. priv = 0 } ,
2008-04-12 09:58:09 -03:00
} ;
/* -- read a register -- */
2009-12-02 14:39:53 -03:00
static u8 reg_r ( struct gspca_dev * gspca_dev ,
2008-07-14 09:38:29 -03:00
__u16 index )
2008-04-12 09:58:09 -03:00
{
struct usb_device * dev = gspca_dev - > dev ;
2008-06-30 15:50:11 -03:00
int ret ;
2008-04-12 09:58:09 -03:00
2009-12-02 14:39:53 -03:00
if ( gspca_dev - > usb_err < 0 )
return 0 ;
2008-04-12 09:58:09 -03:00
ret = usb_control_msg ( dev , usb_rcvctrlpipe ( dev , 0 ) ,
0x00 ,
USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE ,
0x00 ,
index ,
2008-07-14 09:38:29 -03:00
gspca_dev - > usb_buf , 1 ,
2008-04-12 09:58:09 -03:00
500 ) ;
2008-07-14 09:38:29 -03:00
if ( ret < 0 ) {
2011-08-21 19:56:57 -03:00
pr_err ( " reg_r err %d \n " , ret ) ;
2009-12-02 14:39:53 -03:00
gspca_dev - > usb_err = ret ;
return 0 ;
2008-07-14 09:38:29 -03:00
}
return gspca_dev - > usb_buf [ 0 ] ;
2008-04-12 09:58:09 -03:00
}
/* -- write a register -- */
2009-12-02 14:39:53 -03:00
static void reg_w ( struct gspca_dev * gspca_dev ,
2008-04-12 09:58:09 -03:00
__u16 index , __u16 value )
{
struct usb_device * dev = gspca_dev - > dev ;
int ret ;
2009-12-02 14:39:53 -03:00
if ( gspca_dev - > usb_err < 0 )
return ;
2008-04-12 09:58:09 -03:00
ret = usb_control_msg ( dev , usb_sndctrlpipe ( dev , 0 ) ,
0x01 ,
USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE ,
value ,
index ,
NULL ,
0 ,
500 ) ;
2009-12-02 14:39:53 -03:00
if ( ret < 0 ) {
2011-08-21 19:56:57 -03:00
pr_err ( " reg_w err %d \n " , ret ) ;
2009-12-02 14:39:53 -03:00
gspca_dev - > usb_err = ret ;
}
2008-04-12 09:58:09 -03:00
}
2008-07-14 09:38:29 -03:00
/* -- get a bulk value (4 bytes) -- */
2009-12-02 14:39:53 -03:00
static void rcv_val ( struct gspca_dev * gspca_dev ,
2008-07-14 09:38:29 -03:00
int ads )
2008-04-12 09:58:09 -03:00
{
struct usb_device * dev = gspca_dev - > dev ;
int alen , ret ;
2008-06-30 15:50:11 -03:00
reg_w ( gspca_dev , 0x634 , ( ads > > 16 ) & 0xff ) ;
reg_w ( gspca_dev , 0x635 , ( ads > > 8 ) & 0xff ) ;
reg_w ( gspca_dev , 0x636 , ads & 0xff ) ;
reg_w ( gspca_dev , 0x637 , 0 ) ;
2008-07-14 09:38:29 -03:00
reg_w ( gspca_dev , 0x638 , 4 ) ; /* len & 0xff */
reg_w ( gspca_dev , 0x639 , 0 ) ; /* len >> 8 */
2008-06-30 15:50:11 -03:00
reg_w ( gspca_dev , 0x63a , 0 ) ;
reg_w ( gspca_dev , 0x63b , 0 ) ;
reg_w ( gspca_dev , 0x630 , 5 ) ;
2009-12-02 14:39:53 -03:00
if ( gspca_dev - > usb_err < 0 )
return ;
2008-04-12 09:58:09 -03:00
ret = usb_bulk_msg ( dev ,
2008-12-31 08:13:46 -03:00
usb_rcvbulkpipe ( dev , 0x05 ) ,
2008-07-14 09:38:29 -03:00
gspca_dev - > usb_buf ,
4 , /* length */
2008-04-12 09:58:09 -03:00
& alen ,
2008-07-14 09:38:29 -03:00
500 ) ; /* timeout in milliseconds */
2009-12-02 14:39:53 -03:00
if ( ret < 0 ) {
2011-08-21 19:56:57 -03:00
pr_err ( " rcv_val err %d \n " , ret ) ;
2009-12-02 14:39:53 -03:00
gspca_dev - > usb_err = ret ;
}
2008-04-12 09:58:09 -03:00
}
2008-07-14 09:38:29 -03:00
/* -- send a bulk value -- */
2009-12-02 14:39:53 -03:00
static void snd_val ( struct gspca_dev * gspca_dev ,
2008-04-12 09:58:09 -03:00
int ads ,
unsigned int val )
{
struct usb_device * dev = gspca_dev - > dev ;
int alen , ret ;
2008-07-14 09:38:29 -03:00
__u8 seq = 0 ;
2008-04-12 09:58:09 -03:00
if ( ads = = 0x003f08 ) {
2009-12-02 14:39:53 -03:00
reg_r ( gspca_dev , 0x0704 ) ;
seq = reg_r ( gspca_dev , 0x0705 ) ;
reg_r ( gspca_dev , 0x0650 ) ;
2008-06-30 15:50:11 -03:00
reg_w ( gspca_dev , 0x654 , seq ) ;
2008-07-14 09:38:29 -03:00
} else {
2008-06-30 15:50:11 -03:00
reg_w ( gspca_dev , 0x654 , ( ads > > 16 ) & 0xff ) ;
2008-07-14 09:38:29 -03:00
}
2008-06-30 15:50:11 -03:00
reg_w ( gspca_dev , 0x655 , ( ads > > 8 ) & 0xff ) ;
reg_w ( gspca_dev , 0x656 , ads & 0xff ) ;
reg_w ( gspca_dev , 0x657 , 0 ) ;
reg_w ( gspca_dev , 0x658 , 0x04 ) ; /* size */
reg_w ( gspca_dev , 0x659 , 0 ) ;
reg_w ( gspca_dev , 0x65a , 0 ) ;
reg_w ( gspca_dev , 0x65b , 0 ) ;
reg_w ( gspca_dev , 0x650 , 5 ) ;
2009-12-02 14:39:53 -03:00
if ( gspca_dev - > usb_err < 0 )
return ;
2008-07-14 09:38:29 -03:00
gspca_dev - > usb_buf [ 0 ] = val > > 24 ;
gspca_dev - > usb_buf [ 1 ] = val > > 16 ;
gspca_dev - > usb_buf [ 2 ] = val > > 8 ;
gspca_dev - > usb_buf [ 3 ] = val ;
2008-04-12 09:58:09 -03:00
ret = usb_bulk_msg ( dev ,
usb_sndbulkpipe ( dev , 6 ) ,
2008-07-14 09:38:29 -03:00
gspca_dev - > usb_buf ,
2008-04-12 09:58:09 -03:00
4 ,
& alen ,
500 ) ; /* timeout in milliseconds */
2009-12-02 14:39:53 -03:00
if ( ret < 0 ) {
2011-08-21 19:56:57 -03:00
pr_err ( " snd_val err %d \n " , ret ) ;
2009-12-02 14:39:53 -03:00
gspca_dev - > usb_err = ret ;
} else {
if ( ads = = 0x003f08 ) {
seq + = 4 ;
seq & = 0x3f ;
reg_w ( gspca_dev , 0x705 , seq ) ;
}
2008-04-12 09:58:09 -03:00
}
}
/* set a camera parameter */
2009-12-02 14:39:53 -03:00
static void set_par ( struct gspca_dev * gspca_dev ,
2008-04-12 09:58:09 -03:00
int parval )
{
2009-12-02 14:39:53 -03:00
snd_val ( gspca_dev , 0x003f08 , parval ) ;
2008-04-12 09:58:09 -03:00
}
2012-05-16 08:38:34 -03:00
static void setbrightness ( struct gspca_dev * gspca_dev , s32 val )
2008-04-12 09:58:09 -03:00
{
int parval ;
parval = 0x06000000 /* whiteness */
2012-05-16 08:38:34 -03:00
+ ( val < < 16 ) ;
2008-04-12 09:58:09 -03:00
set_par ( gspca_dev , parval ) ;
}
2012-05-16 08:38:34 -03:00
static void setcontrast ( struct gspca_dev * gspca_dev , s32 val )
2008-04-12 09:58:09 -03:00
{
int parval ;
parval = 0x07000000 /* contrast */
2012-05-16 08:38:34 -03:00
+ ( val < < 16 ) ;
2008-04-12 09:58:09 -03:00
set_par ( gspca_dev , parval ) ;
}
2012-05-16 08:38:34 -03:00
static void setcolors ( struct gspca_dev * gspca_dev , s32 val )
2008-04-12 09:58:09 -03:00
{
int parval ;
parval = 0x08000000 /* saturation */
2012-05-16 08:38:34 -03:00
+ ( val < < 16 ) ;
2008-04-12 09:58:09 -03:00
set_par ( gspca_dev , parval ) ;
}
2012-05-16 08:38:34 -03:00
static void setlightfreq ( struct gspca_dev * gspca_dev , s32 val )
2008-06-30 15:50:11 -03:00
{
2012-05-16 08:38:34 -03:00
set_par ( gspca_dev , val = = 1
2008-06-30 15:50:11 -03:00
? 0x33640000 /* 50 Hz */
: 0x33780000 ) ; /* 60 Hz */
}
2008-04-12 09:58:09 -03:00
/* this function is called at probe time */
static int sd_config ( struct gspca_dev * gspca_dev ,
const struct usb_device_id * id )
{
gspca_dev - > cam . cam_mode = vga_mode ;
2008-07-14 09:38:29 -03:00
gspca_dev - > cam . nmodes = ARRAY_SIZE ( vga_mode ) ;
2008-04-12 09:58:09 -03:00
return 0 ;
}
2008-09-03 17:12:16 -03:00
/* this function is called at probe and resume time */
static int sd_init ( struct gspca_dev * gspca_dev )
2008-04-12 09:58:09 -03:00
{
2009-12-02 14:39:53 -03:00
u8 ret ;
2008-04-12 09:58:09 -03:00
/* check if the device responds */
usb_set_interface ( gspca_dev - > dev , gspca_dev - > iface , 1 ) ;
2008-07-14 09:38:29 -03:00
ret = reg_r ( gspca_dev , 0x0740 ) ;
2009-12-02 14:39:53 -03:00
if ( gspca_dev - > usb_err > = 0 ) {
if ( ret ! = 0xff ) {
2011-08-21 19:56:57 -03:00
pr_err ( " init reg: 0x%02x \n " , ret ) ;
2009-12-02 14:39:53 -03:00
gspca_dev - > usb_err = - EIO ;
}
2008-04-12 09:58:09 -03:00
}
2009-12-02 14:39:53 -03:00
return gspca_dev - > usb_err ;
2008-04-12 09:58:09 -03:00
}
/* -- start the camera -- */
2008-09-20 06:39:08 -03:00
static int sd_start ( struct gspca_dev * gspca_dev )
2008-04-12 09:58:09 -03:00
{
2009-03-03 05:33:41 -03:00
struct sd * sd = ( struct sd * ) gspca_dev ;
2008-04-12 09:58:09 -03:00
int ret , value ;
2009-03-03 05:33:41 -03:00
/* create the JPEG header */
2013-08-30 17:54:23 -03:00
jpeg_define ( sd - > jpeg_hdr , gspca_dev - > pixfmt . height ,
gspca_dev - > pixfmt . width ,
2009-03-03 05:33:41 -03:00
0x22 ) ; /* JPEG 411 */
2012-06-27 16:48:33 -03:00
jpeg_set_qual ( sd - > jpeg_hdr , QUALITY ) ;
2009-03-03 05:33:41 -03:00
2008-04-12 09:58:09 -03:00
/* work on alternate 1 */
usb_set_interface ( gspca_dev - > dev , gspca_dev - > iface , 1 ) ;
set_par ( gspca_dev , 0x10000000 ) ;
set_par ( gspca_dev , 0x00000000 ) ;
set_par ( gspca_dev , 0x8002e001 ) ;
set_par ( gspca_dev , 0x14000000 ) ;
2013-08-30 17:54:23 -03:00
if ( gspca_dev - > pixfmt . width > 320 )
2008-04-12 09:58:09 -03:00
value = 0x8002e001 ; /* 640x480 */
else
value = 0x4001f000 ; /* 320x240 */
set_par ( gspca_dev , value ) ;
ret = usb_set_interface ( gspca_dev - > dev ,
gspca_dev - > iface ,
gspca_dev - > alt ) ;
2008-06-30 15:50:11 -03:00
if ( ret < 0 ) {
2011-08-21 19:56:57 -03:00
pr_err ( " set intf %d %d failed \n " ,
gspca_dev - > iface , gspca_dev - > alt ) ;
2009-12-02 14:39:53 -03:00
gspca_dev - > usb_err = ret ;
2008-04-12 09:58:09 -03:00
goto out ;
2008-06-30 15:50:11 -03:00
}
2015-04-29 15:06:20 -03:00
reg_r ( gspca_dev , 0x0630 ) ;
2008-07-14 09:38:29 -03:00
rcv_val ( gspca_dev , 0x000020 ) ; /* << (value ff ff ff ff) */
2009-12-02 14:39:53 -03:00
reg_r ( gspca_dev , 0x0650 ) ;
2008-04-12 09:58:09 -03:00
snd_val ( gspca_dev , 0x000020 , 0xffffffff ) ;
2008-06-30 15:50:11 -03:00
reg_w ( gspca_dev , 0x0620 , 0 ) ;
reg_w ( gspca_dev , 0x0630 , 0 ) ;
reg_w ( gspca_dev , 0x0640 , 0 ) ;
reg_w ( gspca_dev , 0x0650 , 0 ) ;
reg_w ( gspca_dev , 0x0660 , 0 ) ;
2008-04-12 09:58:09 -03:00
set_par ( gspca_dev , 0x09800000 ) ; /* Red ? */
set_par ( gspca_dev , 0x0a800000 ) ; /* Green ? */
set_par ( gspca_dev , 0x0b800000 ) ; /* Blue ? */
set_par ( gspca_dev , 0x0d030000 ) ; /* Gamma ? */
/* start the video flow */
set_par ( gspca_dev , 0x01000000 ) ;
set_par ( gspca_dev , 0x01000000 ) ;
2009-12-02 14:39:53 -03:00
if ( gspca_dev - > usb_err > = 0 )
2017-09-22 15:20:33 -04:00
gspca_dbg ( gspca_dev , D_STREAM , " camera started alt: 0x%02x \n " ,
gspca_dev - > alt ) ;
2008-04-12 09:58:09 -03:00
out :
2009-12-02 14:39:53 -03:00
return gspca_dev - > usb_err ;
2008-04-12 09:58:09 -03:00
}
static void sd_stopN ( struct gspca_dev * gspca_dev )
{
struct usb_device * dev = gspca_dev - > dev ;
set_par ( gspca_dev , 0x02000000 ) ;
set_par ( gspca_dev , 0x02000000 ) ;
usb_set_interface ( dev , gspca_dev - > iface , 1 ) ;
2008-07-14 09:38:29 -03:00
reg_r ( gspca_dev , 0x0630 ) ;
rcv_val ( gspca_dev , 0x000020 ) ; /* << (value ff ff ff ff) */
reg_r ( gspca_dev , 0x0650 ) ;
2008-04-12 09:58:09 -03:00
snd_val ( gspca_dev , 0x000020 , 0xffffffff ) ;
2008-06-30 15:50:11 -03:00
reg_w ( gspca_dev , 0x0620 , 0 ) ;
reg_w ( gspca_dev , 0x0630 , 0 ) ;
reg_w ( gspca_dev , 0x0640 , 0 ) ;
reg_w ( gspca_dev , 0x0650 , 0 ) ;
reg_w ( gspca_dev , 0x0660 , 0 ) ;
2017-09-22 15:20:33 -04:00
gspca_dbg ( gspca_dev , D_STREAM , " camera stopped \n " ) ;
2008-04-12 09:58:09 -03:00
}
static void sd_pkt_scan ( struct gspca_dev * gspca_dev ,
2009-11-13 09:21:03 -03:00
u8 * data , /* isoc packet */
2008-04-12 09:58:09 -03:00
int len ) /* iso packet length */
{
2009-03-03 05:33:41 -03:00
struct sd * sd = ( struct sd * ) gspca_dev ;
2008-05-04 06:46:21 -03:00
static unsigned char ffd9 [ ] = { 0xff , 0xd9 } ;
2008-04-12 09:58:09 -03:00
/* a frame starts with:
* - 0xff 0xfe
2008-06-12 10:58:58 -03:00
* - 0x08 0x00 - length ( little endian ? ! )
* - 4 bytes = size of whole frame ( BE - including header )
2008-04-12 09:58:09 -03:00
* - 0x00 0x0c
* - 0xff 0xd8
* - . . JPEG image with escape sequences ( ff 00 )
2008-06-12 10:58:58 -03:00
* ( without ending - ff d9 )
2008-04-12 09:58:09 -03:00
*/
if ( data [ 0 ] = = 0xff & & data [ 1 ] = = 0xfe ) {
2009-11-13 09:21:03 -03:00
gspca_frame_add ( gspca_dev , LAST_PACKET ,
ffd9 , 2 ) ;
2008-04-12 09:58:09 -03:00
2008-06-12 10:58:58 -03:00
/* put the JPEG 411 header */
2009-11-13 09:21:03 -03:00
gspca_frame_add ( gspca_dev , FIRST_PACKET ,
2009-03-03 05:33:41 -03:00
sd - > jpeg_hdr , JPEG_HDR_SZ ) ;
2008-04-12 09:58:09 -03:00
/* beginning of the frame */
# define STKHDRSZ 12
2008-11-24 06:28:58 -03:00
data + = STKHDRSZ ;
len - = STKHDRSZ ;
2008-04-12 09:58:09 -03:00
}
2009-11-13 09:21:03 -03:00
gspca_frame_add ( gspca_dev , INTER_PACKET , data , len ) ;
2008-04-12 09:58:09 -03:00
}
2012-05-16 08:38:34 -03:00
static int sd_s_ctrl ( struct v4l2_ctrl * ctrl )
{
struct gspca_dev * gspca_dev =
container_of ( ctrl - > handler , struct gspca_dev , ctrl_handler ) ;
gspca_dev - > usb_err = 0 ;
if ( ! gspca_dev - > streaming )
return 0 ;
switch ( ctrl - > id ) {
case V4L2_CID_BRIGHTNESS :
setbrightness ( gspca_dev , ctrl - > val ) ;
break ;
case V4L2_CID_CONTRAST :
setcontrast ( gspca_dev , ctrl - > val ) ;
break ;
case V4L2_CID_SATURATION :
setcolors ( gspca_dev , ctrl - > val ) ;
break ;
case V4L2_CID_POWER_LINE_FREQUENCY :
setlightfreq ( gspca_dev , ctrl - > val ) ;
break ;
}
return gspca_dev - > usb_err ;
}
static const struct v4l2_ctrl_ops sd_ctrl_ops = {
. s_ctrl = sd_s_ctrl ,
} ;
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 ;
2012-06-27 16:48:33 -03:00
v4l2_ctrl_handler_init ( hdl , 4 ) ;
2012-05-16 08:38:34 -03:00
v4l2_ctrl_new_std ( hdl , & sd_ctrl_ops ,
V4L2_CID_BRIGHTNESS , 0 , 255 , 1 , 127 ) ;
v4l2_ctrl_new_std ( hdl , & sd_ctrl_ops ,
V4L2_CID_CONTRAST , 0 , 255 , 1 , 127 ) ;
v4l2_ctrl_new_std ( hdl , & sd_ctrl_ops ,
V4L2_CID_SATURATION , 0 , 255 , 1 , 127 ) ;
v4l2_ctrl_new_std_menu ( hdl , & sd_ctrl_ops ,
V4L2_CID_POWER_LINE_FREQUENCY ,
V4L2_CID_POWER_LINE_FREQUENCY_60HZ , 1 ,
V4L2_CID_POWER_LINE_FREQUENCY_50HZ ) ;
if ( hdl - > error ) {
pr_err ( " Could not initialize controls \n " ) ;
return hdl - > error ;
}
return 0 ;
}
2008-04-12 09:58:09 -03:00
/* sub-driver description */
2008-07-14 09:38:29 -03:00
static const struct sd_desc sd_desc = {
2008-04-12 09:58:09 -03:00
. name = MODULE_NAME ,
. config = sd_config ,
2008-09-03 17:12:16 -03:00
. init = sd_init ,
2012-05-16 08:38:34 -03:00
. init_controls = sd_init_controls ,
2008-04-12 09:58:09 -03:00
. start = sd_start ,
. stopN = sd_stopN ,
. pkt_scan = sd_pkt_scan ,
} ;
/* -- module initialisation -- */
2011-01-13 05:20:29 -03:00
static const struct usb_device_id device_table [ ] = {
2008-07-25 08:53:03 -03:00
{ USB_DEVICE ( 0x05e1 , 0x0893 ) } ,
2008-04-12 09:58:09 -03:00
{ }
} ;
MODULE_DEVICE_TABLE ( usb , device_table ) ;
/* -- device connect -- */
static int sd_probe ( struct usb_interface * intf ,
const struct usb_device_id * id )
{
2008-06-12 10:58:58 -03:00
return gspca_dev_probe ( intf , id , & sd_desc , sizeof ( struct sd ) ,
THIS_MODULE ) ;
2008-04-12 09:58:09 -03:00
}
static struct usb_driver sd_driver = {
. name = MODULE_NAME ,
. id_table = device_table ,
. probe = sd_probe ,
. disconnect = gspca_disconnect ,
2008-09-03 16:48:10 -03:00
# ifdef CONFIG_PM
. suspend = gspca_suspend ,
. resume = gspca_resume ,
2012-06-30 06:44:47 -03:00
. reset_resume = gspca_resume ,
2008-09-03 16:48:10 -03:00
# endif
2008-04-12 09:58:09 -03:00
} ;
2011-11-18 09:46:12 -08:00
module_usb_driver ( sd_driver ) ;