Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-or-later */
0002 /*
0003 
0004     bttv - Bt848 frame grabber driver
0005 
0006     bttv's *private* header file  --  nobody other than bttv itself
0007     should ever include this file.
0008 
0009     (c) 2000-2002 Gerd Knorr <kraxel@bytesex.org>
0010 
0011 */
0012 
0013 #ifndef _BTTVP_H_
0014 #define _BTTVP_H_
0015 
0016 #include <linux/types.h>
0017 #include <linux/wait.h>
0018 #include <linux/i2c.h>
0019 #include <linux/i2c-algo-bit.h>
0020 #include <linux/pci.h>
0021 #include <linux/input.h>
0022 #include <linux/mutex.h>
0023 #include <linux/scatterlist.h>
0024 #include <linux/device.h>
0025 #include <asm/io.h>
0026 #include <media/v4l2-common.h>
0027 #include <media/v4l2-ctrls.h>
0028 #include <media/v4l2-fh.h>
0029 #include <media/videobuf-dma-sg.h>
0030 #include <media/tveeprom.h>
0031 #include <media/rc-core.h>
0032 #include <media/i2c/ir-kbd-i2c.h>
0033 #include <media/drv-intf/tea575x.h>
0034 
0035 #include "bt848.h"
0036 #include "bttv.h"
0037 #include "btcx-risc.h"
0038 
0039 #ifdef __KERNEL__
0040 
0041 #define FORMAT_FLAGS_DITHER       0x01
0042 #define FORMAT_FLAGS_PACKED       0x02
0043 #define FORMAT_FLAGS_PLANAR       0x04
0044 #define FORMAT_FLAGS_RAW          0x08
0045 #define FORMAT_FLAGS_CrCb         0x10
0046 
0047 #define RISC_SLOT_O_VBI        4
0048 #define RISC_SLOT_O_FIELD      6
0049 #define RISC_SLOT_E_VBI       10
0050 #define RISC_SLOT_E_FIELD     12
0051 #define RISC_SLOT_LOOP        14
0052 
0053 #define RESOURCE_OVERLAY       1
0054 #define RESOURCE_VIDEO_STREAM  2
0055 #define RESOURCE_VBI           4
0056 #define RESOURCE_VIDEO_READ    8
0057 
0058 #define RAW_LINES            640
0059 #define RAW_BPL             1024
0060 
0061 #define UNSET (-1U)
0062 
0063 /* Min. value in VDELAY register. */
0064 #define MIN_VDELAY 2
0065 /* Even to get Cb first, odd for Cr. */
0066 #define MAX_HDELAY (0x3FF & -2)
0067 /* Limits scaled width, which must be a multiple of 4. */
0068 #define MAX_HACTIVE (0x3FF & -4)
0069 
0070 #define BTTV_NORMS    (\
0071         V4L2_STD_PAL    | V4L2_STD_PAL_N | \
0072         V4L2_STD_PAL_Nc | V4L2_STD_SECAM | \
0073         V4L2_STD_NTSC   | V4L2_STD_PAL_M | \
0074         V4L2_STD_PAL_60)
0075 /* ---------------------------------------------------------- */
0076 
0077 struct bttv_tvnorm {
0078     int   v4l2_id;
0079     char  *name;
0080     u32   Fsc;
0081     u16   swidth, sheight; /* scaled standard width, height */
0082     u16   totalwidth;
0083     u8    adelay, bdelay, iform;
0084     u32   scaledtwidth;
0085     u16   hdelayx1, hactivex1;
0086     u16   vdelay;
0087     u8    vbipack;
0088     u16   vtotal;
0089     int   sram;
0090     /* ITU-R frame line number of the first VBI line we can
0091        capture, of the first and second field. The last possible line
0092        is determined by cropcap.bounds. */
0093     u16   vbistart[2];
0094     /* Horizontally this counts fCLKx1 samples following the leading
0095        edge of the horizontal sync pulse, vertically ITU-R frame line
0096        numbers of the first field times two (2, 4, 6, ... 524 or 624). */
0097     struct v4l2_cropcap cropcap;
0098 };
0099 extern const struct bttv_tvnorm bttv_tvnorms[];
0100 
0101 struct bttv_format {
0102     int  fourcc;          /* video4linux 2      */
0103     int  btformat;        /* BT848_COLOR_FMT_*  */
0104     int  btswap;          /* BT848_COLOR_CTL_*  */
0105     int  depth;           /* bit/pixel          */
0106     int  flags;
0107     int  hshift,vshift;   /* for planar modes   */
0108 };
0109 
0110 struct bttv_ir {
0111     struct rc_dev           *dev;
0112     struct bttv     *btv;
0113     struct timer_list       timer;
0114 
0115     char                    name[32];
0116     char                    phys[32];
0117 
0118     /* Usual gpio signalling */
0119     u32                     mask_keycode;
0120     u32                     mask_keydown;
0121     u32                     mask_keyup;
0122     u32                     polling;
0123     u32                     last_gpio;
0124     int                     shift_by;
0125     int                     rc5_remote_gap;
0126 
0127     /* RC5 gpio */
0128     bool            rc5_gpio;   /* Is RC5 legacy GPIO enabled? */
0129     u32                     last_bit;   /* last raw bit seen */
0130     u32                     code;       /* raw code under construction */
0131     ktime_t                     base_time;  /* time of last seen code */
0132     bool                    active;     /* building raw code */
0133 };
0134 
0135 
0136 /* ---------------------------------------------------------- */
0137 
0138 struct bttv_geometry {
0139     u8  vtc,crop,comb;
0140     u16 width,hscale,hdelay;
0141     u16 sheight,vscale,vdelay,vtotal;
0142 };
0143 
0144 struct bttv_buffer {
0145     /* common v4l buffer stuff -- must be first */
0146     struct videobuf_buffer     vb;
0147 
0148     /* bttv specific */
0149     const struct bttv_format   *fmt;
0150     unsigned int               tvnorm;
0151     int                        btformat;
0152     int                        btswap;
0153     struct bttv_geometry       geo;
0154     struct btcx_riscmem        top;
0155     struct btcx_riscmem        bottom;
0156     struct v4l2_rect           crop;
0157     unsigned int               vbi_skip[2];
0158     unsigned int               vbi_count[2];
0159 };
0160 
0161 struct bttv_buffer_set {
0162     struct bttv_buffer     *top;       /* top field buffer    */
0163     struct bttv_buffer     *bottom;    /* bottom field buffer */
0164     unsigned int           top_irq;
0165     unsigned int           frame_irq;
0166 };
0167 
0168 struct bttv_overlay {
0169     unsigned int           tvnorm;
0170     struct v4l2_rect       w;
0171     enum v4l2_field        field;
0172     struct v4l2_clip       *clips;
0173     int                    nclips;
0174     int                    setup_ok;
0175 };
0176 
0177 struct bttv_vbi_fmt {
0178     struct v4l2_vbi_format fmt;
0179 
0180     /* fmt.start[] and count[] refer to this video standard. */
0181     const struct bttv_tvnorm *tvnorm;
0182 
0183     /* Earliest possible start of video capturing with this
0184        v4l2_vbi_format, in struct bttv_crop.rect units. */
0185     __s32                  end;
0186 };
0187 
0188 /* bttv-vbi.c */
0189 void bttv_vbi_fmt_reset(struct bttv_vbi_fmt *f, unsigned int norm);
0190 
0191 struct bttv_crop {
0192     /* A cropping rectangle in struct bttv_tvnorm.cropcap units. */
0193     struct v4l2_rect       rect;
0194 
0195     /* Scaled image size limits with this crop rect. Divide
0196        max_height, but not min_height, by two when capturing
0197        single fields. See also bttv_crop_reset() and
0198        bttv_crop_adjust() in bttv-driver.c. */
0199     __s32                  min_scaled_width;
0200     __s32                  min_scaled_height;
0201     __s32                  max_scaled_width;
0202     __s32                  max_scaled_height;
0203 };
0204 
0205 struct bttv_fh {
0206     /* This must be the first field in this struct */
0207     struct v4l2_fh       fh;
0208 
0209     struct bttv              *btv;
0210     int resources;
0211     enum v4l2_buf_type       type;
0212 
0213     /* video capture */
0214     struct videobuf_queue    cap;
0215     const struct bttv_format *fmt;
0216     int                      width;
0217     int                      height;
0218 
0219     /* video overlay */
0220     const struct bttv_format *ovfmt;
0221     struct bttv_overlay      ov;
0222 
0223     /* Application called VIDIOC_S_SELECTION. */
0224     int                      do_crop;
0225 
0226     /* vbi capture */
0227     struct videobuf_queue    vbi;
0228     /* Current VBI capture window as seen through this fh (cannot
0229        be global for compatibility with earlier drivers). Protected
0230        by struct bttv.lock and struct bttv_fh.vbi.lock. */
0231     struct bttv_vbi_fmt      vbi_fmt;
0232 };
0233 
0234 /* ---------------------------------------------------------- */
0235 /* bttv-risc.c                                                */
0236 
0237 /* risc code generators - capture */
0238 int bttv_risc_packed(struct bttv *btv, struct btcx_riscmem *risc,
0239              struct scatterlist *sglist,
0240              unsigned int offset, unsigned int bpl,
0241              unsigned int pitch, unsigned int skip_lines,
0242              unsigned int store_lines);
0243 
0244 /* control dma register + risc main loop */
0245 void bttv_set_dma(struct bttv *btv, int override);
0246 int bttv_risc_init_main(struct bttv *btv);
0247 int bttv_risc_hook(struct bttv *btv, int slot, struct btcx_riscmem *risc,
0248            int irqflags);
0249 
0250 /* capture buffer handling */
0251 int bttv_buffer_risc(struct bttv *btv, struct bttv_buffer *buf);
0252 int bttv_buffer_activate_video(struct bttv *btv,
0253                    struct bttv_buffer_set *set);
0254 int bttv_buffer_activate_vbi(struct bttv *btv,
0255                  struct bttv_buffer *vbi);
0256 void bttv_dma_free(struct videobuf_queue *q, struct bttv *btv,
0257            struct bttv_buffer *buf);
0258 
0259 /* overlay handling */
0260 int bttv_overlay_risc(struct bttv *btv, struct bttv_overlay *ov,
0261               const struct bttv_format *fmt,
0262               struct bttv_buffer *buf);
0263 
0264 
0265 /* ---------------------------------------------------------- */
0266 /* bttv-vbi.c                                                 */
0267 
0268 int bttv_try_fmt_vbi_cap(struct file *file, void *fh, struct v4l2_format *f);
0269 int bttv_g_fmt_vbi_cap(struct file *file, void *fh, struct v4l2_format *f);
0270 int bttv_s_fmt_vbi_cap(struct file *file, void *fh, struct v4l2_format *f);
0271 
0272 extern const struct videobuf_queue_ops bttv_vbi_qops;
0273 
0274 /* ---------------------------------------------------------- */
0275 /* bttv-gpio.c */
0276 
0277 extern struct bus_type bttv_sub_bus_type;
0278 int bttv_sub_add_device(struct bttv_core *core, char *name);
0279 int bttv_sub_del_devices(struct bttv_core *core);
0280 
0281 /* ---------------------------------------------------------- */
0282 /* bttv-cards.c                                               */
0283 
0284 extern int no_overlay;
0285 
0286 /* ---------------------------------------------------------- */
0287 /* bttv-input.c                                               */
0288 
0289 extern void init_bttv_i2c_ir(struct bttv *btv);
0290 
0291 /* ---------------------------------------------------------- */
0292 /* bttv-i2c.c                                                 */
0293 extern int init_bttv_i2c(struct bttv *btv);
0294 extern int fini_bttv_i2c(struct bttv *btv);
0295 
0296 /* ---------------------------------------------------------- */
0297 /* bttv-driver.c                                              */
0298 
0299 /* insmod options */
0300 extern unsigned int bttv_verbose;
0301 extern unsigned int bttv_debug;
0302 extern unsigned int bttv_gpio;
0303 extern void bttv_gpio_tracking(struct bttv *btv, char *comment);
0304 
0305 #define dprintk(fmt, ...)           \
0306 do {                        \
0307     if (bttv_debug >= 1)            \
0308         pr_debug(fmt, ##__VA_ARGS__);   \
0309 } while (0)
0310 #define dprintk_cont(fmt, ...)          \
0311 do {                        \
0312     if (bttv_debug >= 1)            \
0313         pr_cont(fmt, ##__VA_ARGS__);    \
0314 } while (0)
0315 #define d2printk(fmt, ...)          \
0316 do {                        \
0317     if (bttv_debug >= 2)            \
0318         printk(fmt, ##__VA_ARGS__); \
0319 } while (0)
0320 
0321 #define BTTV_MAX_FBUF   0x208000
0322 #define BTTV_TIMEOUT    msecs_to_jiffies(500)    /* 0.5 seconds */
0323 #define BTTV_FREE_IDLE  msecs_to_jiffies(1000)   /* one second */
0324 
0325 
0326 struct bttv_pll_info {
0327     unsigned int pll_ifreq;    /* PLL input frequency        */
0328     unsigned int pll_ofreq;    /* PLL output frequency       */
0329     unsigned int pll_crystal;  /* Crystal used for input     */
0330     unsigned int pll_current;  /* Currently programmed ofreq */
0331 };
0332 
0333 /* for gpio-connected remote control */
0334 struct bttv_input {
0335     struct input_dev      *dev;
0336     char                  name[32];
0337     char                  phys[32];
0338     u32                   mask_keycode;
0339     u32                   mask_keydown;
0340 };
0341 
0342 struct bttv_suspend_state {
0343     u32  gpio_enable;
0344     u32  gpio_data;
0345     int  disabled;
0346     int  loop_irq;
0347     struct bttv_buffer_set video;
0348     struct bttv_buffer     *vbi;
0349 };
0350 
0351 struct bttv_tea575x_gpio {
0352     u8 data, clk, wren, most;
0353 };
0354 
0355 struct bttv {
0356     struct bttv_core c;
0357 
0358     /* pci device config */
0359     unsigned short id;
0360     unsigned char revision;
0361     unsigned char __iomem *bt848_mmio;   /* pointer to mmio */
0362 
0363     /* card configuration info */
0364     unsigned int cardid;   /* pci subsystem id (bt878 based ones) */
0365     unsigned int tuner_type;  /* tuner chip type */
0366     unsigned int tda9887_conf;
0367     unsigned int svhs, dig;
0368     unsigned int has_saa6588:1;
0369     struct bttv_pll_info pll;
0370     int triton1;
0371     int gpioirq;
0372 
0373     int use_i2c_hw;
0374 
0375     /* old gpio interface */
0376     int shutdown;
0377 
0378     void (*volume_gpio)(struct bttv *btv, __u16 volume);
0379     void (*audio_mode_gpio)(struct bttv *btv, struct v4l2_tuner *tuner, int set);
0380 
0381     /* new gpio interface */
0382     spinlock_t gpio_lock;
0383 
0384     /* i2c layer */
0385     struct i2c_algo_bit_data   i2c_algo;
0386     struct i2c_client          i2c_client;
0387     int                        i2c_state, i2c_rc;
0388     int                        i2c_done;
0389     wait_queue_head_t          i2c_queue;
0390     struct v4l2_subdev    *sd_msp34xx;
0391     struct v4l2_subdev    *sd_tvaudio;
0392     struct v4l2_subdev    *sd_tda7432;
0393 
0394     /* video4linux (1) */
0395     struct video_device video_dev;
0396     struct video_device radio_dev;
0397     struct video_device vbi_dev;
0398 
0399     /* controls */
0400     struct v4l2_ctrl_handler   ctrl_handler;
0401     struct v4l2_ctrl_handler   radio_ctrl_handler;
0402 
0403     /* infrared remote */
0404     int has_remote;
0405     struct bttv_ir *remote;
0406 
0407     /* I2C remote data */
0408     struct IR_i2c_init_data    init_data;
0409 
0410     /* locking */
0411     spinlock_t s_lock;
0412     struct mutex lock;
0413     int resources;
0414 
0415     /* video state */
0416     unsigned int input;
0417     unsigned int audio_input;
0418     unsigned int mute;
0419     unsigned long tv_freq;
0420     unsigned int tvnorm;
0421     v4l2_std_id std;
0422     int hue, contrast, bright, saturation;
0423     struct v4l2_framebuffer fbuf;
0424     unsigned int field_count;
0425 
0426     /* various options */
0427     int opt_combfilter;
0428     int opt_automute;
0429     int opt_vcr_hack;
0430     int opt_uv_ratio;
0431 
0432     /* radio data/state */
0433     int has_radio;
0434     int has_radio_tuner;
0435     int radio_user;
0436     int radio_uses_msp_demodulator;
0437     unsigned long radio_freq;
0438 
0439     /* miro/pinnacle + Aimslab VHX
0440        philips matchbox (tea5757 radio tuner) support */
0441     int has_tea575x;
0442     struct bttv_tea575x_gpio tea_gpio;
0443     struct snd_tea575x tea;
0444 
0445     /* ISA stuff (Terratec Active Radio Upgrade) */
0446     int mbox_ior;
0447     int mbox_iow;
0448     int mbox_csel;
0449 
0450     /* switch status for multi-controller cards */
0451     char sw_status[4];
0452 
0453     /* risc memory management data
0454        - must acquire s_lock before changing these
0455        - only the irq handler is supported to touch top + bottom + vcurr */
0456     struct btcx_riscmem     main;
0457     struct bttv_buffer      *screen;    /* overlay             */
0458     struct list_head        capture;    /* video capture queue */
0459     struct list_head        vcapture;   /* vbi capture queue   */
0460     struct bttv_buffer_set  curr;       /* active buffers      */
0461     struct bttv_buffer      *cvbi;      /* active vbi buffer   */
0462     int                     loop_irq;
0463     int                     new_input;
0464 
0465     unsigned long cap_ctl;
0466     unsigned long dma_on;
0467     struct timer_list timeout;
0468     struct bttv_suspend_state state;
0469 
0470     /* stats */
0471     unsigned int errors;
0472     unsigned int framedrop;
0473     unsigned int irq_total;
0474     unsigned int irq_me;
0475 
0476     unsigned int users;
0477     struct bttv_fh init;
0478 
0479     /* used to make dvb-bt8xx autoloadable */
0480     struct work_struct request_module_wk;
0481 
0482     /* Default (0) and current (1) video capturing and overlay
0483        cropping parameters in bttv_tvnorm.cropcap units. Protected
0484        by bttv.lock. */
0485     struct bttv_crop crop[2];
0486 
0487     /* Earliest possible start of video capturing in
0488        bttv_tvnorm.cropcap line units. Set by check_alloc_btres()
0489        and free_btres(). Protected by bttv.lock. */
0490     __s32           vbi_end;
0491 
0492     /* Latest possible end of VBI capturing (= crop[x].rect.top when
0493        VIDEO_RESOURCES are locked). Set by check_alloc_btres()
0494        and free_btres(). Protected by bttv.lock. */
0495     __s32           crop_start;
0496 };
0497 
0498 static inline struct bttv *to_bttv(struct v4l2_device *v4l2_dev)
0499 {
0500     return container_of(v4l2_dev, struct bttv, c.v4l2_dev);
0501 }
0502 
0503 /* our devices */
0504 #define BTTV_MAX 32
0505 extern unsigned int bttv_num;
0506 extern struct bttv *bttvs[BTTV_MAX];
0507 
0508 static inline unsigned int bttv_muxsel(const struct bttv *btv,
0509                        unsigned int input)
0510 {
0511     return (bttv_tvcards[btv->c.type].muxsel >> (input * 2)) & 3;
0512 }
0513 
0514 #endif
0515 
0516 #define btwrite(dat,adr)    writel((dat), btv->bt848_mmio+(adr))
0517 #define btread(adr)         readl(btv->bt848_mmio+(adr))
0518 
0519 #define btand(dat,adr)      btwrite((dat) & btread(adr), adr)
0520 #define btor(dat,adr)       btwrite((dat) | btread(adr), adr)
0521 #define btaor(dat,mask,adr) btwrite((dat) | ((mask) & btread(adr)), adr)
0522 
0523 #endif /* _BTTVP_H_ */