Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-only */
0002 /*
0003  * Hauppauge HD PVR USB driver
0004  *
0005  * Copyright (C) 2008      Janne Grunau (j@jannau.net)
0006  */
0007 
0008 #include <linux/usb.h>
0009 #include <linux/i2c.h>
0010 #include <linux/mutex.h>
0011 #include <linux/workqueue.h>
0012 #include <linux/videodev2.h>
0013 
0014 #include <media/v4l2-device.h>
0015 #include <media/v4l2-ctrls.h>
0016 #include <media/i2c/ir-kbd-i2c.h>
0017 
0018 #define HDPVR_MAX 8
0019 #define HDPVR_I2C_MAX_SIZE 128
0020 
0021 /* Define these values to match your devices */
0022 #define HD_PVR_VENDOR_ID    0x2040
0023 #define HD_PVR_PRODUCT_ID   0x4900
0024 #define HD_PVR_PRODUCT_ID1  0x4901
0025 #define HD_PVR_PRODUCT_ID2  0x4902
0026 #define HD_PVR_PRODUCT_ID4  0x4903
0027 #define HD_PVR_PRODUCT_ID3  0x4982
0028 
0029 #define UNSET    (-1U)
0030 
0031 #define NUM_BUFFERS 64
0032 
0033 #define HDPVR_FIRMWARE_VERSION      0x08
0034 #define HDPVR_FIRMWARE_VERSION_AC3  0x0d
0035 #define HDPVR_FIRMWARE_VERSION_0X12 0x12
0036 #define HDPVR_FIRMWARE_VERSION_0X15 0x15
0037 #define HDPVR_FIRMWARE_VERSION_0X1E 0x1e
0038 
0039 /* #define HDPVR_DEBUG */
0040 
0041 extern int hdpvr_debug;
0042 
0043 #define MSG_INFO    1
0044 #define MSG_BUFFER  2
0045 
0046 struct hdpvr_options {
0047     u8  video_std;
0048     u8  video_input;
0049     u8  audio_input;
0050     u8  bitrate;    /* in 100kbps */
0051     u8  peak_bitrate;   /* in 100kbps */
0052     u8  bitrate_mode;
0053     u8  gop_mode;
0054     enum v4l2_mpeg_audio_encoding   audio_codec;
0055     u8  brightness;
0056     u8  contrast;
0057     u8  hue;
0058     u8  saturation;
0059     u8  sharpness;
0060 };
0061 
0062 /* Structure to hold all of our device specific stuff */
0063 struct hdpvr_device {
0064     /* the v4l device for this device */
0065     struct video_device video_dev;
0066     /* the control handler for this device */
0067     struct v4l2_ctrl_handler hdl;
0068     /* the usb device for this device */
0069     struct usb_device   *udev;
0070     /* v4l2-device unused */
0071     struct v4l2_device  v4l2_dev;
0072     struct { /* video mode/bitrate control cluster */
0073         struct v4l2_ctrl *video_mode;
0074         struct v4l2_ctrl *video_bitrate;
0075         struct v4l2_ctrl *video_bitrate_peak;
0076     };
0077     /* v4l2 format */
0078     uint width, height;
0079 
0080     /* the max packet size of the bulk endpoint */
0081     size_t          bulk_in_size;
0082     /* the address of the bulk in endpoint */
0083     __u8            bulk_in_endpointAddr;
0084 
0085     /* holds the current device status */
0086     __u8            status;
0087 
0088     /* holds the current set options */
0089     struct hdpvr_options    options;
0090     v4l2_std_id     cur_std;
0091     struct v4l2_dv_timings  cur_dv_timings;
0092 
0093     uint            flags;
0094 
0095     /* synchronize I/O */
0096     struct mutex        io_mutex;
0097     /* available buffers */
0098     struct list_head    free_buff_list;
0099     /* in progress buffers */
0100     struct list_head    rec_buff_list;
0101     /* waitqueue for buffers */
0102     wait_queue_head_t   wait_buffer;
0103     /* waitqueue for data */
0104     wait_queue_head_t   wait_data;
0105     /**/
0106     struct work_struct  worker;
0107     /* current stream owner */
0108     struct v4l2_fh      *owner;
0109 
0110     /* I2C adapter */
0111     struct i2c_adapter  i2c_adapter;
0112     /* I2C lock */
0113     struct mutex        i2c_mutex;
0114     /* I2C message buffer space */
0115     char            i2c_buf[HDPVR_I2C_MAX_SIZE];
0116 
0117     /* For passing data to ir-kbd-i2c */
0118     struct IR_i2c_init_data ir_i2c_init_data;
0119 
0120     /* usb control transfer buffer and lock */
0121     struct mutex        usbc_mutex;
0122     u8          *usbc_buf;
0123     u8          fw_ver;
0124 };
0125 
0126 static inline struct hdpvr_device *to_hdpvr_dev(struct v4l2_device *v4l2_dev)
0127 {
0128     return container_of(v4l2_dev, struct hdpvr_device, v4l2_dev);
0129 }
0130 
0131 
0132 /* buffer one bulk urb of data */
0133 struct hdpvr_buffer {
0134     struct list_head    buff_list;
0135 
0136     struct urb      *urb;
0137 
0138     struct hdpvr_device *dev;
0139 
0140     uint            pos;
0141 
0142     __u8            status;
0143 };
0144 
0145 /* */
0146 
0147 struct hdpvr_video_info {
0148     u16 width;
0149     u16 height;
0150     u8  fps;
0151     bool    valid;
0152 };
0153 
0154 enum {
0155     STATUS_UNINITIALIZED    = 0,
0156     STATUS_IDLE,
0157     STATUS_STARTING,
0158     STATUS_SHUTTING_DOWN,
0159     STATUS_STREAMING,
0160     STATUS_ERROR,
0161     STATUS_DISCONNECTED,
0162 };
0163 
0164 enum {
0165     HDPVR_FLAG_AC3_CAP = 1,
0166 };
0167 
0168 enum {
0169     BUFSTAT_UNINITIALIZED = 0,
0170     BUFSTAT_AVAILABLE,
0171     BUFSTAT_INPROGRESS,
0172     BUFSTAT_READY,
0173 };
0174 
0175 #define CTRL_START_STREAMING_VALUE  0x0700
0176 #define CTRL_STOP_STREAMING_VALUE   0x0800
0177 #define CTRL_BITRATE_VALUE      0x1000
0178 #define CTRL_BITRATE_MODE_VALUE     0x1200
0179 #define CTRL_GOP_MODE_VALUE     0x1300
0180 #define CTRL_VIDEO_INPUT_VALUE      0x1500
0181 #define CTRL_VIDEO_STD_TYPE     0x1700
0182 #define CTRL_AUDIO_INPUT_VALUE      0x2500
0183 #define CTRL_BRIGHTNESS         0x2900
0184 #define CTRL_CONTRAST           0x2a00
0185 #define CTRL_HUE            0x2b00
0186 #define CTRL_SATURATION         0x2c00
0187 #define CTRL_SHARPNESS          0x2d00
0188 #define CTRL_LOW_PASS_FILTER_VALUE  0x3100
0189 
0190 #define CTRL_DEFAULT_INDEX      0x0003
0191 
0192 
0193     /* :0 s 38 01 1000 0003 0004 4 = 0a00ca00
0194      * BITRATE SETTING
0195      *   1st and 2nd byte (little endian): average bitrate in 100 000 bit/s
0196      *                                     min: 1 mbit/s, max: 13.5 mbit/s
0197      *   3rd and 4th byte (little endian): peak bitrate in 100 000 bit/s
0198      *                                     min: average + 100kbit/s,
0199      *                                      max: 20.2 mbit/s
0200      */
0201 
0202     /* :0 s 38 01 1200 0003 0001 1 = 02
0203      * BIT RATE MODE
0204      *  constant = 1, variable (peak) = 2, variable (average) = 3
0205      */
0206 
0207     /* :0 s 38 01 1300 0003 0001 1 = 03
0208      * GOP MODE (2 bit)
0209      *    low bit 0/1: advanced/simple GOP
0210      *   high bit 0/1: IDR(4/32/128) / no IDR (4/32/0)
0211      */
0212 
0213     /* :0 s 38 01 1700 0003 0001 1 = 00
0214      * VIDEO STANDARD or FREQUENCY 0 = 60hz, 1 = 50hz
0215      */
0216 
0217     /* :0 s 38 01 3100 0003 0004 4 = 03030000
0218      * FILTER CONTROL
0219      *   1st byte luma low pass filter strength,
0220      *   2nd byte chroma low pass filter strength,
0221      *   3rd byte MF enable chroma, min=0, max=1
0222      *   4th byte n
0223      */
0224 
0225 
0226     /* :0 s 38 b9 0001 0000 0000 0 */
0227 
0228 
0229 
0230 /* :0 s 38 d3 0000 0000 0001 1 = 00 */
0231 /*      ret = usb_control_msg(dev->udev, */
0232 /*                    usb_sndctrlpipe(dev->udev, 0), */
0233 /*                    0xd3, 0x38, */
0234 /*                    0, 0, */
0235 /*                    "\0", 1, */
0236 /*                    1000); */
0237 
0238 /*      info("control request returned %d", ret); */
0239 /*      msleep(5000); */
0240 
0241 
0242     /* :0 s b8 81 1400 0003 0005 5 <
0243      * :0 0 5 = d0024002 19
0244      * QUERY FRAME SIZE AND RATE
0245      *   1st and 2nd byte (little endian): horizontal resolution
0246      *   3rd and 4th byte (little endian): vertical resolution
0247      *   5th byte: frame rate
0248      */
0249 
0250     /* :0 s b8 81 1800 0003 0003 3 <
0251      * :0 0 3 = 030104
0252      * QUERY SIGNAL AND DETECTED LINES, maybe INPUT
0253      */
0254 
0255 enum hdpvr_video_std {
0256     HDPVR_60HZ = 0,
0257     HDPVR_50HZ,
0258 };
0259 
0260 enum hdpvr_video_input {
0261     HDPVR_COMPONENT = 0,
0262     HDPVR_SVIDEO,
0263     HDPVR_COMPOSITE,
0264     HDPVR_VIDEO_INPUTS
0265 };
0266 
0267 enum hdpvr_audio_inputs {
0268     HDPVR_RCA_BACK = 0,
0269     HDPVR_RCA_FRONT,
0270     HDPVR_SPDIF,
0271     HDPVR_AUDIO_INPUTS
0272 };
0273 
0274 enum hdpvr_bitrate_mode {
0275     HDPVR_CONSTANT = 1,
0276     HDPVR_VARIABLE_PEAK,
0277     HDPVR_VARIABLE_AVERAGE,
0278 };
0279 
0280 enum hdpvr_gop_mode {
0281     HDPVR_ADVANCED_IDR_GOP = 0,
0282     HDPVR_SIMPLE_IDR_GOP,
0283     HDPVR_ADVANCED_NOIDR_GOP,
0284     HDPVR_SIMPLE_NOIDR_GOP,
0285 };
0286 
0287 void hdpvr_delete(struct hdpvr_device *dev);
0288 
0289 /*========================================================================*/
0290 /* hardware control functions */
0291 int hdpvr_set_options(struct hdpvr_device *dev);
0292 
0293 int hdpvr_set_bitrate(struct hdpvr_device *dev);
0294 
0295 int hdpvr_set_audio(struct hdpvr_device *dev, u8 input,
0296             enum v4l2_mpeg_audio_encoding codec);
0297 
0298 int hdpvr_config_call(struct hdpvr_device *dev, uint value,
0299               unsigned char valbuf);
0300 
0301 int get_video_info(struct hdpvr_device *dev, struct hdpvr_video_info *vid_info);
0302 
0303 /* :0 s b8 81 1800 0003 0003 3 < */
0304 /* :0 0 3 = 0301ff */
0305 int get_input_lines_info(struct hdpvr_device *dev);
0306 
0307 
0308 /*========================================================================*/
0309 /* v4l2 registration */
0310 int hdpvr_register_videodev(struct hdpvr_device *dev, struct device *parent,
0311                 int devnumber);
0312 
0313 int hdpvr_cancel_queue(struct hdpvr_device *dev);
0314 
0315 /*========================================================================*/
0316 /* i2c adapter registration */
0317 int hdpvr_register_i2c_adapter(struct hdpvr_device *dev);
0318 
0319 struct i2c_client *hdpvr_register_ir_i2c(struct hdpvr_device *dev);
0320 
0321 /*========================================================================*/
0322 /* buffer management */
0323 int hdpvr_free_buffers(struct hdpvr_device *dev);
0324 int hdpvr_alloc_buffers(struct hdpvr_device *dev, uint count);